package com.cloudpivot.receivales.ding.userinfo.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.cloudpivot.receivales.ding.basic.service.DingBasicService;
import com.cloudpivot.receivales.ding.userinfo.service.DingUserService;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiV2UserGetRequest;
import com.dingtalk.api.request.OapiV2UserGetbymobileRequest;
import com.dingtalk.api.response.OapiV2UserGetResponse;
import com.dingtalk.api.response.OapiV2UserGetbymobileResponse;
import com.taobao.api.ApiException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
@Slf4j
public class DingUserServiceImpl implements DingUserService {

    @Autowired
    private DingBasicService dingBasicService;

    /**
     * 通过用户phone 查询userid
     *
     * @param phone
     * @return
     */
    @Override
    public String useridByMobilPhone(String phone) {
        if (StringUtils.isBlank(phone)) {
            return null;
        }
        //获取token
        try {
            String access_token = dingBasicService.getToken("dinggroup");

            DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/getbymobile");
            OapiV2UserGetbymobileRequest req = new OapiV2UserGetbymobileRequest();
            req.setMobile(phone);
            OapiV2UserGetbymobileResponse rsp = client.execute(req, access_token);
            OapiV2UserGetbymobileResponse.UserGetByMobileResponse rspResult = rsp.getResult();
            return rspResult.getUserid();

        } catch (ApiException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 获取用户unionid
     *
     * @param phone
     * @return
     */
    @Override
    public String getUnionidByMobilePhone(String phone) {
        String access_token = dingBasicService.getToken("dinggroup");

        try {
            DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/getbymobile");
            OapiV2UserGetbymobileRequest req = new OapiV2UserGetbymobileRequest();
            req.setMobile(phone);
            OapiV2UserGetbymobileResponse rsp = client.execute(req, access_token);
            OapiV2UserGetbymobileResponse.UserGetByMobileResponse rspResult = rsp.getResult();
            String userid = rspResult.getUserid();

            client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/get");
            OapiV2UserGetRequest userReq = new OapiV2UserGetRequest();
            userReq.setUserid(userid);
            OapiV2UserGetResponse unionRsp = client.execute(userReq, access_token);
            return unionRsp.getResult().getUnionid();
        } catch (ApiException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public List<String> convertListPhoneToUserid(List<String> list) {
        List<String> reList = new ArrayList<>();
        if (Objects.isNull(list)) {
            return null;
        }
        for (String s : list) {
            String s1 = useridByMobilPhone(s);
            if (StringUtils.isNotBlank(s1)) {
                reList.add(s1);
            }
        }
        return reList;
    }

    @Override
    public String dingUidToUnionId(String uid) {
        try {
            String access_token = dingBasicService.getToken("dinggroup");

            DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/get");
            OapiV2UserGetRequest req = new OapiV2UserGetRequest();
            req.setUserid(uid);
            req.setLanguage("zh_CN");
            OapiV2UserGetResponse rsp = client.execute(req, access_token);
            System.out.println(rsp.getBody());
            JSONObject jsonObject = JSONObject.parseObject(rsp.getBody());
            JSONObject userDTO=jsonObject.getJSONObject("result");

            return userDTO.getString("unionid");
        } catch (ApiException e) {
            e.printStackTrace();
            System.out.println("dingUidToUnionId服务异常--->"+e.toString());
        }
        return null;
    }
}
